package com.cch.httpserver.ex02;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Locale;

/**
 * Description:
 * Author: Hank
 * Date: 2018/8/5 23:04
 */

public class Response implements ServletResponse{
    private static  final int BUFFER_SIZE = 1024;
    Request request;
    OutputStream output;
    PrintWriter writer;

    public Response(OutputStream output) {
        this.output = output;
    }

    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    public void sendStaticResource()throws IOException{
        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        try {

            if(request.getUri()==null){
                errWrite();
                return;
            }
            File file = new File(HttpServer1.WEB_ROOT, request.getUri());
            if(file.exists()){
                fis = new FileInputStream(file);
                int ch = fis.read(bytes,0,BUFFER_SIZE);
                String result ="HTTP/1.1 200 OK\r\n" +
                        "Content-Type: text/html;charset=UTF-8\r\n" +
                        "\r\n";
                output.write(result.getBytes());
                while(ch!= -1){
                    output.write(bytes,0,ch);
                    ch = fis.read(bytes,0,BUFFER_SIZE);
                }
                output.flush();
            }else{
                errWrite();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fis!=null){
                fis.close();
            }
        }
    }

    private void errWrite() throws IOException {
        String errMessage="HTTP/1.1 404 File Not Found\r\n"+
                "Content-type: text/html\r\n"+
                "Content-length: 23\r\n"+
                "\r\n"+
                "<h1>File Not Found</h1>";
        output.write(errMessage.getBytes());
        System.out.println("not found");
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        writer = new PrintWriter(output,true);
        return writer;
    }

    @Override
    public void setCharacterEncoding(String charset) {

    }

    @Override
    public void setContentLength(int len) {

    }

    @Override
    public void setContentType(String type) {

    }

    @Override
    public void setBufferSize(int size) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale loc) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }
}
